package com.aimaterials.resource.task;

import com.aimaterials.common.utils.StringUtils;
import com.aimaterials.resource.domain.ResResource;
import com.aimaterials.resource.service.IResResourceService;
import com.aimaterials.resource.util.FFmpegCommandException;
import com.aimaterials.resource.util.FFmpegUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 视频任务定时处理器
 * @author DaemonSu
 */
@Component("videoHandleTask")
public class VideoHandleTask {

    @Autowired
    private IResResourceService resResourceService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void handleVideo()
    {
        //查询所有没有处理的视频
        List<ResResource> list=resResourceService.selectUNResourceList(".mp4");
       Iterator<ResResource> i=list.iterator();
        while(i.hasNext()){
            try {
                ResResource res=i.next();
                //视频资源进行处理
                Map<String,String> map=FFmpegUtil.handle(res);
                if(map.isEmpty()){
                    continue;
                }
                String filePath=res.getResourceUrl();
                StringBuilder sb=new StringBuilder(filePath.substring(0,filePath.lastIndexOf(".")));

                String fileName=map.get("fileName");
                sb.append("/").append(fileName);
                res.setPreview(sb.toString()+".png");
                res.setStatus("1");
                res.setResourceUrl(sb.toString()+".m3u8");
                //处理之后设置
                resResourceService.updateVideo(res);
            } catch (FFmpegCommandException e) {
                e.printStackTrace();
            }
        }
        //将所有视频放入队列进行处
        System.out.println("执行无参方法");
    }
}
